using APP.HELPER.Data.Helper;
using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Common.Class
{
	public class TransRef
	{
		private string m_ID;
		private string m_RefID;
		private string m_RefOrgNo;
		private int m_RefType;
		private DateTime m_RefDate;
		private string m_TransFrom;
		private string m_TransTo;
		private string m_Department_ID;
		private string m_Employee_ID;
		private string m_Stock_ID;
		private string m_Branch_ID;
		private string m_Contract_ID;
		private string m_Contract;
		private string m_Reason;
		private string m_Currency_ID;
		private decimal m_ExchangeRate;
		private decimal m_Amount;
		private string m_Description;
		private string m_User_ID;
		[Category("Column"), DisplayName("Discount")]
		public decimal Discount
		{
			get;
			set;
		}
		[Category("Column"), DisplayName("FAmount")]
		public decimal FAmount
		{
			get;
			set;
		}
		[Category("Column"), DisplayName("FDiscount")]
		public decimal FDiscount
		{
			get;
			set;
		}
		[Category("Column"), DisplayName("IsClose")]
		public bool IsClose
		{
			get;
			set;
		}
		[Category("Column"), DisplayName("Sorted")]
		public long Sorted
		{
			get;
			set;
		}
		[Category("Column"), DisplayName("Active")]
		public bool Active
		{
			get;
			set;
		}
		public TransRef()
		{
			this.m_ID = "";
			this.m_RefID = "";
			this.m_RefOrgNo = "";
			this.m_RefType = 0;
			this.m_RefDate = DateTime.Now;
			this.m_TransFrom = "";
			this.m_TransTo = "";
			this.m_Department_ID = "";
			this.m_Employee_ID = "";
			this.m_Stock_ID = "";
			this.m_Branch_ID = "";
			this.m_Contract_ID = "";
			this.m_Contract = "";
			this.m_Reason = "";
			this.m_Currency_ID = "";
			this.m_ExchangeRate = 0m;
			this.m_Amount = 0m;
			this.Discount = 0m;
			this.FAmount = 0m;
			this.FDiscount = 0m;
			this.IsClose = true;
			this.Sorted = 0L;
			this.m_Description = "";
			this.m_User_ID = "";
			this.Active = true;
		}
		public TransRef(string ID, string RefID, string RefOrgNo, int RefType, DateTime RefDate, string TransFrom, string TransTo, string Department_ID, string Employee_ID, string Stock_ID, string Branch_ID, string Contract_ID, string Contract, string Reason, string Currency_ID, decimal ExchangeRate, decimal Amount, decimal Discount, decimal FAmount, decimal FDiscount, bool IsClose, long Sorted, string Description, string User_ID, bool Active)
		{
			this.m_ID = ID;
			this.m_RefID = RefID;
			this.m_RefOrgNo = RefOrgNo;
			this.m_RefType = RefType;
			this.m_RefDate = RefDate;
			this.m_TransFrom = TransFrom;
			this.m_TransTo = TransTo;
			this.m_Department_ID = Department_ID;
			this.m_Employee_ID = Employee_ID;
			this.m_Stock_ID = Stock_ID;
			this.m_Branch_ID = Branch_ID;
			this.m_Contract_ID = Contract_ID;
			this.m_Contract = Contract;
			this.m_Reason = Reason;
			this.m_Currency_ID = Currency_ID;
			this.m_ExchangeRate = ExchangeRate;
			this.m_Amount = Amount;
			this.Discount = Discount;
			this.FAmount = FAmount;
			this.FDiscount = FDiscount;
			this.IsClose = IsClose;
			this.Sorted = Sorted;
			this.m_Description = Description;
			this.m_User_ID = User_ID;
			this.Active = Active;
		}
		public void SetData(string ID, string RefID, string RefOrgNo, int RefType, DateTime RefDate, string TransFrom, string TransTo, string Department_ID, string Employee_ID, string Stock_ID, string Branch_ID, string Contract_ID, string Contract, string Reason, string Currency_ID, decimal ExchangeRate, decimal Amount, decimal Discount, decimal FAmount, decimal FDiscount, bool IsClose, long Sorted, string Description, string User_ID, bool Active)
		{
			this.m_ID = ID;
			this.m_RefID = RefID;
			this.m_RefOrgNo = RefOrgNo;
			this.m_RefType = RefType;
			this.m_RefDate = RefDate;
			this.m_TransFrom = TransFrom;
			this.m_TransTo = TransTo;
			this.m_Department_ID = Department_ID;
			this.m_Employee_ID = Employee_ID;
			this.m_Stock_ID = Stock_ID;
			this.m_Branch_ID = Branch_ID;
			this.m_Contract_ID = Contract_ID;
			this.m_Contract = Contract;
			this.m_Reason = Reason;
			this.m_Currency_ID = Currency_ID;
			this.m_ExchangeRate = ExchangeRate;
			this.m_Amount = Amount;
			this.Discount = Discount;
			this.FAmount = FAmount;
			this.FDiscount = FDiscount;
			this.IsClose = IsClose;
			this.Sorted = Sorted;
			this.m_Description = Description;
			this.m_User_ID = User_ID;
			this.Active = Active;
		}
		public string NewID(string key)
		{
			return SqlHelper.GenCode("TRANS_REF", "RefID", key);
		}
		public bool Exist(string ID)
		{
			SqlHelper sqlHelper = new SqlHelper();
			sqlHelper.Error += new SqlHelper.ErrorEventHander(this.DispError);
			bool result;
			if (sqlHelper.Open() != "OK")
			{
				result = false;
			}
			else
			{
				if (this.Exist(sqlHelper.Connection, ID))
				{
					sqlHelper.Close();
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}
		public bool Exist(SqlConnection myConnection, string ID)
		{
			bool result = false;
			string[] myParams = new string[]
			{
				"@RefID"
			};
			object[] myValues = new object[]
			{
				ID
			};
			SqlHelper sqlHelper = new SqlHelper();
			SqlDataReader sqlDataReader = sqlHelper.ExecuteReader(myConnection, "TRANS_REF_ExistByID", myParams, myValues);
			if (sqlDataReader != null)
			{
				result = sqlDataReader.HasRows;
				sqlDataReader.Close();
			}
			else
			{
				sqlHelper.Close();
				if (MyParamaters.IsShowWarning)
				{
					XtraMessageBox.Show(sqlHelper.Result, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			return result;
		}
		public bool Exist(SqlTransaction myTransaction, string ID)
		{
			bool result = false;
			string[] myParams = new string[]
			{
				"@RefID"
			};
			object[] myValues = new object[]
			{
				ID
			};
			SqlHelper sqlHelper = new SqlHelper();
			SqlDataReader sqlDataReader = sqlHelper.ExecuteReader(myTransaction, "TRANS_REF_ExistByID", myParams, myValues);
			if (sqlDataReader != null)
			{
				result = sqlDataReader.HasRows;
				sqlDataReader.Close();
			}
			else
			{
				sqlHelper.RollBack(myTransaction);
				if (MyParamaters.IsShowWarning)
				{
					XtraMessageBox.Show(sqlHelper.Result, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			return result;
		}
		public bool Insert(SqlTransaction myTransaction)
		{
			return this.Insert(myTransaction, this.m_ID, this.m_RefID, this.m_RefOrgNo, this.m_RefType, this.m_RefDate, this.m_TransFrom, this.m_TransTo, this.m_Department_ID, this.m_Employee_ID, this.m_Stock_ID, this.m_Branch_ID, this.m_Contract_ID, this.m_Contract, this.m_Reason, this.m_Currency_ID, this.m_ExchangeRate, this.m_Amount, this.Discount, this.FAmount, this.FDiscount, this.IsClose, this.Sorted, this.m_Description, this.m_User_ID, this.Active);
		}
		public bool Insert(SqlConnection myConnection, string ID, string RefID, string RefOrgNo, int RefType, DateTime RefDate, string TransFrom, string TransTo, string Department_ID, string Employee_ID, string Stock_ID, string Branch_ID, string Contract_ID, string Contract, string Reason, string Currency_ID, decimal ExchangeRate, decimal Amount, decimal Discount, decimal FAmount, decimal FDiscount, bool IsClose, long Sorted, string Description, string User_ID, bool Active)
		{
			string[] myParams = new string[]
			{
				"@ID",
				"@RefID",
				"@RefOrgNo",
				"@RefType",
				"@RefDate",
				"@TransFrom",
				"@TransTo",
				"@Department_ID",
				"@Employee_ID",
				"@Stock_ID",
				"@Branch_ID",
				"@Contract_ID",
				"@Contract",
				"@Reason",
				"@Currency_ID",
				"@ExchangeRate",
				"@Amount",
				"@Discount",
				"@FAmount",
				"@FDiscount",
				"@IsClose",
				"@Sorted",
				"@Description",
				"@User_ID",
				"@Active"
			};
			object[] myValues = new object[]
			{
				ID,
				RefID,
				RefOrgNo,
				RefType,
				RefDate,
				TransFrom,
				TransTo,
				Department_ID,
				Employee_ID,
				Stock_ID,
				Branch_ID,
				Contract_ID,
				Contract,
				Reason,
				Currency_ID,
				ExchangeRate,
				Amount,
				Discount,
				FAmount,
				FDiscount,
				IsClose,
				Sorted,
				Description,
				User_ID,
				Active
			};
			SqlHelper sqlHelper = new SqlHelper();
			sqlHelper.Error += new SqlHelper.ErrorEventHander(this.DispError);
			return sqlHelper.ExecuteNonQuery(myConnection, "TRANS_REF_Insert", myParams, myValues) == "OK";
		}
		public bool Insert(SqlTransaction myTransaction, string ID, string RefID, string RefOrgNo, int RefType, DateTime RefDate, string TransFrom, string TransTo, string Department_ID, string Employee_ID, string Stock_ID, string Branch_ID, string Contract_ID, string Contract, string Reason, string Currency_ID, decimal ExchangeRate, decimal Amount, decimal Discount, decimal FAmount, decimal FDiscount, bool IsClose, long Sorted, string Description, string User_ID, bool Active)
		{
			string[] myParams = new string[]
			{
				"@ID",
				"@RefID",
				"@RefOrgNo",
				"@RefType",
				"@RefDate",
				"@TransFrom",
				"@TransTo",
				"@Department_ID",
				"@Employee_ID",
				"@Stock_ID",
				"@Branch_ID",
				"@Contract_ID",
				"@Contract",
				"@Reason",
				"@Currency_ID",
				"@ExchangeRate",
				"@Amount",
				"@Discount",
				"@FAmount",
				"@FDiscount",
				"@IsClose",
				"@Sorted",
				"@Description",
				"@User_ID",
				"@Active"
			};
			object[] myValues = new object[]
			{
				ID,
				RefID,
				RefOrgNo,
				RefType,
				RefDate,
				TransFrom,
				TransTo,
				Department_ID,
				Employee_ID,
				Stock_ID,
				Branch_ID,
				Contract_ID,
				Contract,
				Reason,
				Currency_ID,
				ExchangeRate,
				Amount,
				Discount,
				FAmount,
				FDiscount,
				IsClose,
				Sorted,
				Description,
				User_ID,
				Active
			};
			SqlHelper sqlHelper = new SqlHelper();
			sqlHelper.Error += new SqlHelper.ErrorEventHander(this.DispError);
			return sqlHelper.ExecuteNonQuery(myTransaction, "TRANS_REF_Insert", myParams, myValues) == "OK";
		}
		public bool Update(SqlTransaction myTransaction)
		{
			return this.Update(myTransaction, this.m_ID, this.m_RefID, this.m_RefOrgNo, this.m_RefType, this.m_RefDate, this.m_TransFrom, this.m_TransTo, this.m_Department_ID, this.m_Employee_ID, this.m_Stock_ID, this.m_Branch_ID, this.m_Contract_ID, this.m_Contract, this.m_Reason, this.m_Currency_ID, this.m_ExchangeRate, this.m_Amount, this.Discount, this.FAmount, this.FDiscount, this.IsClose, this.Sorted, this.m_Description, this.m_User_ID, this.Active);
		}
		public bool Update(SqlTransaction myTransaction, string ID, string RefID, string RefOrgNo, int RefType, DateTime RefDate, string TransFrom, string TransTo, string Department_ID, string Employee_ID, string Stock_ID, string Branch_ID, string Contract_ID, string Contract, string Reason, string Currency_ID, decimal ExchangeRate, decimal Amount, decimal Discount, decimal FAmount, decimal FDiscount, bool IsClose, long Sorted, string Description, string User_ID, bool Active)
		{
			string[] myParams = new string[]
			{
				"@ID",
				"@RefID",
				"@RefOrgNo",
				"@RefType",
				"@RefDate",
				"@TransFrom",
				"@TransTo",
				"@Department_ID",
				"@Employee_ID",
				"@Stock_ID",
				"@Branch_ID",
				"@Contract_ID",
				"@Contract",
				"@Reason",
				"@Currency_ID",
				"@ExchangeRate",
				"@Amount",
				"@Discount",
				"@FAmount",
				"@FDiscount",
				"@IsClose",
				"@Description",
				"@User_ID",
				"@Active"
			};
			object[] myValues = new object[]
			{
				ID,
				RefID,
				RefOrgNo,
				RefType,
				RefDate,
				TransFrom,
				TransTo,
				Department_ID,
				Employee_ID,
				Stock_ID,
				Branch_ID,
				Contract_ID,
				Contract,
				Reason,
				Currency_ID,
				ExchangeRate,
				Amount,
				Discount,
				FAmount,
				FDiscount,
				IsClose,
				Description,
				User_ID,
				Active
			};
			SqlHelper sqlHelper = new SqlHelper();
			sqlHelper.Error += new SqlHelper.ErrorEventHander(this.DispError);
			return sqlHelper.ExecuteNonQuery(myTransaction, "TRANS_REF_UpdateById", myParams, myValues) == "OK";
		}
		private void DispError(object sender, SqlHelperException e)
		{
			XtraMessageBox.Show(e.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
		}
		public DataTable GetListByAll()
		{
			SqlHelper sqlHelper = new SqlHelper();
			return sqlHelper.ExecuteDataTable("TRANS_REF_GetListAll");
		}
		public DataTable GetList(DateTime from, DateTime to)
		{
			string[] myParams = new string[]
			{
				"@From",
				"@To"
			};
			object[] myValues = new object[]
			{
				from,
				to
			};
			SqlHelper sqlHelper = new SqlHelper();
			return sqlHelper.ExecuteDataTable("TRANS_REF_GetList_ByDate", myParams, myValues);
		}
		public string UpdateBySorted(SqlTransaction myTransaction, string id, long sorted)
		{
			string[] myParams = new string[]
			{
				"@ID",
				"@Sorted"
			};
			object[] myValues = new object[]
			{
				id,
				sorted
			};
			SqlHelper sqlHelper = new SqlHelper();
			return sqlHelper.ExecuteNonQuery(myTransaction, "TRANS_REF_UpdateBySorted", myParams, myValues);
		}
		public DataTable GetProductHistory(string productid, string productName)
		{
			SqlHelper sqlHelper = new SqlHelper();
			sqlHelper.Error += new SqlHelper.ErrorEventHander(this.DispError);
			sqlHelper.CommandType = CommandType.Text;
			string commandText = string.Concat(new string[]
			{
				"SELECT id.RefDate,\r\n                           id.RefNo,\r\n                           id.RefType,\r\n                           id.RefStatus,\r\n                           id.Product_ID,\r\n                           id.Product_Name,\r\n                           UNIT.Unit_Name,\r\n                           id.Quantity,\r\n                           id.Price as UnitPrice,\r\n                           (id.Price * id.Quantity) as Amount,\r\n                           id.Stock_ID,\r\n                           id.Customer_ID,\r\n                           id.Description,\r\n                           id.Employee_ID\r\n                    FROM INVENTORY_DETAIL id\r\n                           LEFT JOIN PRODUCT\r\n                                ON  id.Product_ID = PRODUCT.Product_ID\r\n                           LEFT JOIN UNIT\r\n                                ON  id.Unit = UNIT.Unit_ID\r\n                    WHERE  (id.Product_ID LIKE N'%",
				productid,
				"%')\r\n                           AND (id.Product_Name LIKE N'%",
				productName,
				"%')\r\n                    ORDER BY\r\n                           id.RefDate"
			});
			return sqlHelper.ExecuteDataTable(commandText);
		}
	}
}
